/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.client.ui;

import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.layout.VLayout;
import org.gwtportlets.portlet.client.WidgetFactory;
import org.gwtportlets.portlet.client.ui.Portlet;
import org.gwtportlets.portlet.smartgwt.client.DataTransferObject;
import org.gwtportlets.portlet.smartgwt.client.SmartPortlet;
import org.gwtportlets.portlet.smartgwt.client.SmartPortletDataSource;
import org.gwtportlets.portlet.smartgwt.client.SmartPortletFactory;
import za.org.foobar.famon.frontend.client.Famon;
import za.org.foobar.famon.frontend.client.dto.*;

/**
 * @author Carl Crous
 */
public class FailureBrowserPortlet extends SmartPortlet {
    private ApplicationFailureListGrid applicationGroupGrid;
    private final SmartPortletDataSource failureDataSource;
    private final SmartPortletDataSource applicationGroupDataSource;
    private final SmartPortletDataSource threadDataSource;
    private final SmartPortletDataSource loggingEventDataSource;
    private final SmartPortletDataSource stackTraceElementDataSource;

    public FailureBrowserPortlet() {
        failureDataSource = FailureDto.getDataSource(this);
        applicationGroupDataSource = ApplicationGroupDto.getDataSource(this);
        threadDataSource = ThreadDto.getDataSource(this);
        loggingEventDataSource = LoggingEventDto.getDataSource(this);
        stackTraceElementDataSource = StackTraceElementDto.getDataSource(this);

        applicationGroupGrid = (ApplicationFailureListGrid)Canvas.getById(ApplicationFailureListGrid.COMPONENT_ID);
        if (applicationGroupGrid == null) {
            applicationGroupGrid = new ApplicationFailureListGrid(this);
        }

        Criteria c = new Criteria();
        String applicationName = Famon.getInstance().getPage().getParameter("application_name");
        if (applicationName != null) {
            c.addCriteria("applicationName", applicationName);
        }
        String instanceName = Famon.getInstance().getPage().getParameter("instance_name");
        if (instanceName != null) {
            c.addCriteria("instanceName", instanceName);
        }
        String versionName = Famon.getInstance().getPage().getParameter("version_name");
        if (versionName != null) {
            c.addCriteria("versionName", versionName);
        }
        applicationGroupGrid.setInitialCriteria(c);

        VLayout layout = new VLayout(15);
        layout.addMember(applicationGroupGrid);

        initWidget(layout);
    }

    public WidgetFactory createWidgetFactory() {
        return new Factory();
    }

    public static class Factory extends SmartPortletFactory<FailureBrowserPortlet> {

        public Factory() {
        }

        public Factory(Portlet p) {
            super(p);
        }

        public FailureBrowserPortlet createWidget() {
            return new FailureBrowserPortlet();
        }

        public DataTransferObject createDto() {
            if (ApplicationGroupDto.DATA_SOURCE_ID.equals(dataSourceId)) {
                return new ApplicationGroupDto();
            } else if (FailureDto.DATA_SOURCE_ID.equals(dataSourceId)) {
                return new FailureDto();
            } else if (ThreadDto.DATA_SOURCE_ID.equals(dataSourceId)) {
                return new ThreadDto();
            } else if (StackTraceElementDto.DATA_SOURCE_ID.equals(dataSourceId)) {
                return new StackTraceElementDto();
            } else if (LoggingEventDto.DATA_SOURCE_ID.equals(dataSourceId)) {
                return new LoggingEventDto();
            }
            return null;
        }
    }
}
